package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.common.protocol.types.Field;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    private WmChannelMapper wmChannelMapper;
    @Autowired
    private WmNewsMapper wmNewsMapper;
    @Override
    public ResponseResult channelList() {
        //1.分页构造(安全考虑)
        Page<WmChannel> page = new Page<>(1,100);
        //2.查询构造
        Page<WmChannel> pageResult = wmChannelMapper.selectPage(page, null);;
        //3.返回
        return ResponseResult.okResult(pageResult.getRecords());
    }

    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {
        //0.判空
        if (wmChannel==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //1.获取频道名并判空
        String name = wmChannel.getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"频道名为空");
        }
        //2.查询是否重复
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmChannel::getName,name);
        Integer findResult = wmChannelMapper.selectCount(wrapper);
        if(findResult>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道名重复");
        }
        //3.新增频道名
        int insertResult = wmChannelMapper.insert(wmChannel);
        if(insertResult<1){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"添加失败");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public PageResponseResult findList(ChannelDto dto) {
        //0.参数判空
        if (dto==null) {
            return new PageResponseResult(0,0,0);
        }
        //1.入参信息
        String name = dto.getName();
        Integer page = dto.getPage();
        Integer size = dto.getSize();
        //2.入参判空
        page=page==null?1:page;
        size=size==null?10:size;
        //3.构建查询条件
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name),WmChannel::getName,name);
        wrapper.orderByDesc(WmChannel::getCreatedTime);
        //4.查询,组装并返回
        Page<WmChannel> pageInfo = new Page<>(page, size);
        Page<WmChannel> pageResult = wmChannelMapper.selectPage(pageInfo, wrapper);
        Long total = pageResult.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(page, size, total.intValue());
        pageResponseResult.setData(pageResult.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        //0.参数判空
        if (wmChannel==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //1.获取入参
        String name = wmChannel.getName();
        Integer id = wmChannel.getId();
        //2.频道名重复查询
        LambdaQueryWrapper<WmChannel> wrapperName = new LambdaQueryWrapper<>();
        wrapperName.eq(StringUtils.isNotBlank(name),WmChannel::getName,name);
        Integer nameCountResult = wmChannelMapper.selectCount(wrapperName);
        if (nameCountResult>0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道名重复");
        }
        //3.频道牵连查询
        LambdaQueryWrapper<WmNews> wrapperWmnews = new LambdaQueryWrapper<>();
        wrapperWmnews.eq(id!=null,WmNews::getChannelId,id);
        wrapperWmnews.eq(WmNews::getStatus,9);
        Integer wmnewsCountResult= wmNewsMapper.selectCount(wrapperWmnews);
        if(wmnewsCountResult>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道下有文章,不能修改");
        }
        //4.修改并返回
        wmChannelMapper.updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        //0.判断入参是否为空
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //1.查询频道信息
        WmChannel wmChannel = wmChannelMapper.selectById(id);
        if (wmChannel==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.判断频道状态
        if(wmChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"启动中的频道不能删除");
        }
        //3.检查频道下是否有新闻
        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmNews::getChannelId,id);
        Integer wmnewsCountReslut = wmNewsMapper.selectCount(wrapper);
        if(wmnewsCountReslut!=0){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"频道关联了新闻,不能删除");
        }
        //4.删除
        wmChannelMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }
}